<?php
class Administrator_user_model extends CI_Model {

    function __construct()
    {
        parent::__construct();
    }	
	
	function user_management_add()
	{
		$group_id = $this->input->post('group_id');
		$username = $this->input->post('username');
		$password = $this->input->post('password');
		$status = $this->input->post('status');
		$firstname = $this->input->post('firstname');
		$surname = $this->input->post('surname');
		$nickname = $this->input->post('nickname');
		$phone = $this->input->post('phone');
		$email = $this->input->post('email');
		$datetime = date('Y-m-d H:i:s');
		
		/*Insert user*/
		$data = array(
			'group_id' => $group_id,
			'username' => $username,
			'password' => $password,
			'status' => $status,
			'create_date' => $datetime,
			'update_date' => $datetime
		);
		$this->db->insert('user',$data);	
			
		$insert_id = $this->db->insert_id();
		
		/*Insert user_info*/
		$data = array(
			'user_id' => $insert_id,
			'firstname' => $firstname,
			'surname' => $surname,
			'nickname' => $nickname,
			'phone' => $phone,
			'email' => $email
		);
		$this->db->insert('user_info',$data);	
	}
	
	function user_management_edit($id)
	{
		$group_id = $this->input->post('group_id');
		$username = $this->input->post('username');
		$password = $this->input->post('password');
		$status = $this->input->post('status');
		$firstname = $this->input->post('firstname');
		$surname = $this->input->post('surname');
		$nickname = $this->input->post('nickname');
		$phone = $this->input->post('phone');
		$email = $this->input->post('email');
		$datetime = date('Y-m-d H:i:s');
		
		/*Update user*/
		$data = array(
			'group_id' => $group_id,
			'username' => $username,
			'password' => $password,
			'status' => $status,
			'update_date' => $datetime
		);
		$this->db->update('user',$data,array('user_id' => $id));
		
		/*Update user_info*/
		$data = array(
			'firstname' => $firstname,
			'surname' => $surname,
			'nickname' => $nickname,
			'phone' => $phone,
			'email' => $email
		);
		$this->db->update('user_info',$data,array('user_id' => $id));		
	}	
	
	function user_management_delete()
	{
		$delete_menu = $this->input->post('delete_menu');
		foreach($delete_menu as $value):
			if($value != 1): //if not admin ID(ID = 1)
				$tables = array('user','user_info');
				$this->db->where('user_id', $value);
				$this->db->delete($tables);
			endif;	
		endforeach;
	}	
	
	function ajax_username_valid()
	{
		$username = $this->input->post('username');
		$old_username = $this->input->post('old_username');
		//Check allow character
		$disallow_character = array('administrator','user','username','password','webmaster','webmasters','support','supports','sale','sales','moderator');
		if(preg_match('/[^a-zA-Z0-9]+/',$username) || in_array($username,$disallow_character)):
			$response =  1;
		endif;
		//Exist Database		
		$this->db->from('user');
		$this->db->where('username',$username);
		if(!empty($old_username)) $this->db->where_not_in('username',$old_username);
		$is_exist = $this->db->count_all_results();
		if(!empty($is_exist)):
			$response =  1;
		endif;
		echo $response;
	}
	
	function ajax_email_valid()
	{
		$email = $this->input->post('email');
		$old_email = $this->input->post('old_email');
		$this->db->from('user_info');
		$this->db->where('email',$email);
		if(!empty($old_email)) $this->db->where_not_in('email',$old_email);		
		$is_exist = $this->db->count_all_results();
		if(!empty($is_exist)):
			echo '1';
		endif;			
	}	
	
	function user_profile_edit($id)
	{
		$password = $this->input->post('newpassword');
		$firstname = $this->input->post('firstname');
		$surname = $this->input->post('surname');
		$nickname = $this->input->post('nickname');
		$phone = $this->input->post('phone');
		$email = $this->input->post('email');
		$datetime = date('Y-m-d H:i:s');
		
		/*Update user*/
		$data = array(
			'update_date' => $datetime
		);
		if(!empty($password)) $data['password'] = $password;
		$this->db->update('user',$data,array('user_id' => $id));
		
		/*Update user_info*/
		$data = array(
			'firstname' => $firstname,
			'surname' => $surname,
			'nickname' => $nickname,
			'phone' => $phone,
			'email' => $email
		);
		$this->db->update('user_info',$data,array('user_id' => $id));		
	}	
}

?>